package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.CurrencyDto;
import com.wizz.trade.information.dto.page.CurrencyPageDto;
import com.wizz.trade.information.entity.Currency;
import com.wizz.trade.information.service.CurrencyService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.vo.CurrencyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 资料模块/币种信息
 */
@RestController
@RequestMapping("/currency")
public class CurrencyController {

    @Resource
    private CurrencyService currencyService;

    /**
     * 添加操作
     * @param currencyDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult addCurrency(@RequestBody CurrencyDto currencyDto){
        Currency currency = new Currency();
        BeanUtils.copyProperties(currencyDto,currency);
        currencyService.addCurrency(currency);
        return new ResponseResult(StatusCode.OK,"添加成功");
    }

    /**
     * 更新（编辑）操作
     * @param currencyDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult updateCurrency(@RequestBody CurrencyDto currencyDto, @PathVariable("id") Integer id){
        Currency currency = new Currency();
        BeanUtils.copyProperties(currencyDto,currency);
        currency.setId(id);
        currencyService.updateCurrency(currency);
        return new ResponseResult(StatusCode.OK,"修改成功");
    }

    /**
     * 删除（撤销）操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult deleteCurrency(@PathVariable("id") Integer id){
        currencyService.deleteCurrency(id);
        return new ResponseResult(StatusCode.OK,"删除成功");
    }

    /**
     * 分页刷新、条件查询
     * @param currencyPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('设置币种、订单模式')")

    public ResponseResult<PageVo<CurrencyVo>> list(CurrencyPageDto currencyPageDto){
        PageVo<Currency> rawPage = currencyService.getPage(currencyPageDto);
        List<Currency> data = rawPage.getData();
        List<CurrencyVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            CurrencyVo  currencyVo = new CurrencyVo();
            BeanUtils.copyProperties(data.get(i), currencyVo);
            dataVo.add(currencyVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取下拉框选项
     * @return 选项
     */
    @GetMapping("/options")
    public ResponseResult<List<Map<Integer, String>>> getOptions(){
        return new ResponseResult<>(StatusCode.OK, currencyService.getOptions());
    }

}
